package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.util.JsonResult;
import cn.wolfcode.luowowo.common.util.KeysUtil;
import cn.wolfcode.luowowo.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.service.util.StatisUtil;
import cn.wolfcode.luowowo.vo.StatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Set;

@Service
public class TravelStatisRedisServiceImpl implements ITravelStatisRedisService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Reference
    private ITravelService travelService;

    @Autowired
    private StatisUtil util;
    @Override
    public void statisAndUpdateViewnum(Long id, int i, int type) {
        util.statisAndUpdateViewnum(id, i, type);
    }

    @Override
    public StatisVO getStatisRedis(String key) {
        return util.getStatisRedis(key);
    }

    @Override
    public JsonResult favor(Long sId, Long uId, int userFavorType) {
        return util.favor(sId, uId, userFavorType);
    }

    @Override
    public JsonResult strategyThumbup(Long sid, Long userInfoId, int userFavorType ) {
        return util.strategyThumbup(sid, userInfoId, userFavorType);
    }

    @Override
    public void setReplynumStatisRedis(Long detailId, int i, String sKey) {
        util.setReplynumStatisRedis(detailId, i, sKey);
    }

    @Override
    public boolean checkFavor(Long id, String ukey) {
        return util.checkFavor(id,ukey);
    }

    public void initRedisStatis() {
        List<Travel> travels = travelService.list();
        for (Travel travel : travels) {
            String key = KeysUtil.TRAVEL_STATIS_VO.join(travel.getId().toString());
            if (redisTemplate.hasKey(key)) {
                continue;
            }
            StatisVO vo = new StatisVO();
            util.copyTravelProperties(travel, vo);
            redisTemplate.opsForValue().set(key, JSON.toJSONString(vo));
        }
    }

    @Override
    public void redisPersistent() {
        Set<String> TravelKeys = redisTemplate.keys(KeysUtil.TRAVEL_STATIS_VO.getName() + "*");
        for (String key : TravelKeys) {
            StatisVO vo = JSON.parseObject(redisTemplate.opsForValue().get(key), StatisVO.class);
            Travel travel = new Travel();
            try {
                BeanUtils.copyProperties(travel, vo);
                travel.setId(vo.getStrategyId());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            travelService.updateRedisStatus(travel);
        }
    }
}
